package web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Follow;
import beans.Member;
import beans.PersonInformation;
import beans.TypeMessage;
import beans.Typer;
import service.FollowService;
import service.TypeMessageService;
import service.TypeService;
import serviceimpl.FollowServiceIpmt;
import serviceimpl.SearchPeopleIpmt;
import serviceimpl.TypeMessageServiceIpml;
import serviceimpl.TypeServiceIpmt;

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet("/HomeServlet")
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Member member = (Member) request.getSession().getAttribute("member");
		TypeService ts = new TypeServiceIpmt();
		Typer typer = ts.showType(member.getUsername());
		TypeMessageService tm = new TypeMessageServiceIpml();
		if(typer!=null) {
			TypeMessage typeMessageBytype = tm.getTypeMessageBytype(typer.getType());
			request.getSession().setAttribute("typeMessageBytype", typeMessageBytype);
		}
		FollowService fs = new FollowServiceIpmt();
		SearchPeopleIpmt service = new SearchPeopleIpmt ();
		List<Follow> list =  fs.followUsername(member.getUsername());
		List<PersonInformation> p = new ArrayList<PersonInformation>();
		for(Follow f:list) {
			p.add(service.findMemberByusername(f.getFollowusername()));
		}
		request.getSession().setAttribute("p", p);

		response.sendRedirect("/zzLoveProduct/home/home.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
